<template>
  <div class="containers">
    <div class="container">
      <div class="form-box" style="width: 100%;max-width: 900px;">
        <el-tabs >
          <el-tab-pane label="微信支付设置" >
            <el-form ref="form"  label-width="120px">

              <el-form-item label="类型">
                <el-select v-model="form.types" @change="getData()" placeholder="请选择类型" size="small">
                  <el-option label="app" :value="0"> </el-option>
                  <el-option label="h5" :value="1"> </el-option>
                  <el-option label="微信小程序" :value="2"> </el-option>
                  <el-option label="微信公众号" :value="3"> </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="是否开启">
                <el-switch :active-value="1" :inactive-value="0" v-model="form.is_open"></el-switch>
              </el-form-item>
              <el-form-item label="微信appID">
                <el-input v-model="form.appid"></el-input>
              </el-form-item>
              <el-form-item label="密钥">
                <el-input v-model="form.secret"></el-input>
              </el-form-item>
              <el-form-item label="微信商户号">
                <el-input v-model="form.merchant"></el-input>
              </el-form-item>
              <el-form-item label="微信序列号">
                <el-input v-model="form.serial"></el-input>
              </el-form-item>
              <el-form-item label="V3 密钥">
                <el-input v-model="form.v3"></el-input>
              </el-form-item>
              <el-form-item label="回调URL">
                <el-input v-model="form.notifyUrl"></el-input>
              </el-form-item>
              <el-form-item label="上传文件">
                <input type="file" ref="filElem" @change="onChange">
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="setlv()">确 定</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ShibangHtRelease',

  data() {
    return {
      apiclient_key:'',
      form:{
        is_open:0,
        types:0,
        appid:'',
        secret:'',
        merchant:'',
        serial:'',
        v3:'',
        apiclient_key:'',
        code:"1",
        notifyUrl:''
      },
      upimg: {
        token: '',
        key: new Date().valueOf()
      },
      upload:'',
      formdata:'',
    };
  },
  created(){
    this.getData()
  },
  mounted() {

  },

  methods: {
    onChange(event) {
      console.log(event,'5555')
      var that = this;
      const inputFile = this.$refs.filElem.files[0];
      this.form.apiclient_key = inputFile
      this.formdata = new FormData()
      this.formdata.append('appid', that.form.appid)
      this.formdata.append('is_open', that.form.is_open)
      this.formdata.append('secret', that.form.secret)
      this.formdata.append('merchant', that.form.merchant)
      this.formdata.append('serial', that.form.serial)
      this.formdata.append('v3', that.form.v3)
      this.formdata.append('notifyUrl', that.form.notifyUrl)
      if(inputFile){
        this.formdata.append('apiclient_key', that.form.apiclient_key)
        this.formdata.append('code', that.form.code?1:1)
        that.form.code=1
      }else{
        that.form.code=0
        this.formdata.append('code', that.form.code?0:0)
      }
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      this.form.apiclient_key = this.url + res.key;
      this.apiclient_key = URL.createObjectURL(file.raw);
    },
    beforeupload(file) {
      let types = file.name.split('.')//获取图片格式
      this.upimg.key = this.$template+'/wxpay/' + new Date().valueOf() + '.' + types[types.length-1];
    },
    setlv() {
      const inputFile = this.$refs.filElem.files[0];
      if(this.form.code==1&&inputFile==undefined){
        this.$message.error('请上传文件');
        return
      }
      let updata=this.form
      this.$http({
        url:  `${this.mainurl}admin/wxSet/?types=${updata.types}&is_open=${updata.is_open}&code=${inputFile==undefined?0:1}&notifyUrl=${updata.notifyUrl}&appid=${updata.appid}&secret=${updata.secret}&merchant=${updata.merchant}&serial=${updata.serial}&v3=${updata.v3}`,
        method: 'post',
        headers: {
          'Content-Type': 'multipart/form-data',
          'token': localStorage.getItem('admin_token_sys')
        },
        data: this.formdata,
        dataType: "json",
        cache: false, //上传文件无需缓存
        processData: false, //用于对data参数进行序列化处理 这里必须false
        contentType: false, //必须
      }).then(res => {
        this.$message.success(res.data.errmsg)
        this.getData()
      })
    },
    getData(){
      let types=this.form.types
      this.request('admin/wxSet/?types='+types, 'GET').then(res => {
        // if(){
        this.form = res.data
        this.form.types=types
        // }
      })
    }
  },
};
</script>

<style lang="scss" scoped>

</style>
